import time

from selenium.webdriver.support.ui import WebDriverWait

class Base():
    # 初始化
    def __init__(self,driver):
        self.driver = driver
    # 封装网址
    def open_url(self,url):
        self.driver.get(url)
    # 时间戳
    def get_time(self):
        em = time.strftime('%Y-%m-%d  %H:%M:%S',time.localtime())
        self.driver.save_screenshot('../Png'+'Img'+em+'.png')
    # 封装定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,1).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
            self.get_time()
    # 点击事件
    def click(self,loc):
        self.find_element(loc).click()
    # 文本输入 send_keys
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 文本输出 text
    def text(self,loc):
        return self.find_element(loc).text
    # iframe 进入
    def iframe(self,loc):
        self.find_element(loc).switch_to.frame()
    # iframe 退出
    def iframe_uncon(self,loc):
        self.find_element(loc).switch_to.default_content()
    # 多窗口切换
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 滚动到底部
    def down(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
